package com.yakumosakura.yakumoblade.event;

import com.yakumosakura.yakumoblade.registry.SAchangeList;
import com.yakumosakura.yakumoblade.registry.TextureChangeList;
import com.yakumosakura.yakumoblade.registry.slashblade.YASpecialEffectsRegistry;
import com.yakumosakura.yakumoblade.utils.SlashBladeUtil;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.slasharts.SlashArts;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.RegistryObject;

import java.util.List;
import java.util.Optional;

@Mod.EventBusSubscriber
public class ChangeTypeEvent {
    public static Boolean change = true;

    @SubscribeEvent
    public static void ChangeType(TypeMakerCanChange event) {
        Player player = event.getEntity();
        Optional<ISlashBladeState> stateOptional = SlashBladeUtil.getState(player.getMainHandItem());

        if (!stateOptional.isPresent()) return;

        ISlashBladeState state = stateOptional.get();
        SlashArts sa = state.getSlashArts();

        changeCrow(sa, state, player);
        changeGap(sa, state, player);
        changeDead(sa, state, player);
        changeYamato(sa, state, player);
        changeDragon(sa, state, player);
        changeFox(sa, state, player);
        change = true;
    }

    private static void changeSlashArts(SlashArts currentArts, ISlashBladeState state, Player player,
                                        List<RegistryObject<SlashArts>> artsList, ResourceLocation effectId) {
        changeSlashArts(currentArts, state, player, artsList, effectId,null);
    }
    private static void changeSlashArts(SlashArts currentArts, ISlashBladeState state, Player player,
                                        List<RegistryObject<SlashArts>> artsList, ResourceLocation effectId,List<ResourceLocation> texture) {
        if (!state.hasSpecialEffect(effectId)) return;
        int currentIndex = -1;
        for (int i = 0; i < artsList.size(); i++) {
            if (currentArts == artsList.get(i).get()) {
                currentIndex = i;
                break;
            }
        }
        if (currentIndex != -1) {
            int nextIndex = (currentIndex + 1) % artsList.size();
            RegistryObject<SlashArts> nextArts = artsList.get(nextIndex);
            if (texture!=null){
                state.setTexture(texture.get(nextIndex));
            }
            state.setSlashArtsKey(nextArts.getId());
            if (nextArts.getId()!=null){
            player.displayClientMessage(Component.literal(createMessage(player, nextArts.getId(), state)), true);
            }
        } else if (!artsList.isEmpty()) {
            state.setSlashArtsKey(artsList.get(0).getId());
            if (texture!=null){
                state.setTexture(texture.get(0));
            }

                player.displayClientMessage(Component.literal(createMessage(player, artsList.get(0).getId(), state)), true);

        }
    }

    public static void changeDead(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.YUYUKO_SA,
                YASpecialEffectsRegistry.SwordArtOnDead.getId());
    }

    public static void changeGap(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.YUKARI_SA,
                YASpecialEffectsRegistry.SwordArtOnGap.getId());
    }

    public static void changeCrow(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.CROW_SA,
                YASpecialEffectsRegistry.SwordArtOnCrow.getId());
    }
    public static void changeYamato(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.VERGIL_SA,
                YASpecialEffectsRegistry.SwordArtOnDMC.getId());
    }
    public static void changeDragon(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.DRAGON_HEXGRAM_SA,
                YASpecialEffectsRegistry.SwordArtOnDRAGON.getId(), TextureChangeList.DRAGON_GRAMTEXTURE);
    }
    public static void changeFox(SlashArts currentArts, ISlashBladeState state, Player player) {
        changeSlashArts(currentArts, state, player, SAchangeList.FOX_HEXGRAM_SA,
                YASpecialEffectsRegistry.SwordArtOnFOX.getId(), TextureChangeList.FOX_GRAMTEXTURE);
    }
    public static String createMessage(Player player, ResourceLocation sa, ISlashBladeState state) {
        return Component.translatable(state.getTranslationKey()).getString()
                + Component.translatable("yakumoblade.types.open").getString()
                + Component.translatable("slash_art." + sa.toLanguageKey()).getString();
    }
}